
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections;

using Swaf;
using Swaf.BizObj.basic;

namespace Swaf.BizObj
{

    [Serializable]
	public class BizObjField : IBizObjField, ICloneable
	{
		protected IBizObjDefinition m_parent;
		protected int m_parentIndex;
		protected string m_name;
		protected BizFieldType m_bizFieldType;
		protected object m_defaultValue;
		protected string m_actualFieldType;
		protected IList m_validators;
		protected IDictionary m_decorators;
		protected bool m_inheritedField;
		protected bool m_hasStandins = false;
		protected Type m_platformType = null;

		protected static IDictionary s_emptyDecos = new Hashtable();

		public BizObjField(IBizObjDefinition parent, int parentIndex, string name, BizFieldType bizFieldType, object defaultValue, string actualFieldType, bool inheritedField)
		{
			m_parent = parent;
			m_parentIndex = parentIndex;

			m_name = name;
			m_bizFieldType = bizFieldType;
			m_defaultValue = defaultValue;
			if (m_defaultValue != null)
				m_platformType = m_defaultValue.GetType();
			else
			{
				IClassFactory factory = Application.globalApp.factories["BizObjBizFieldTypes"];
				object initValue = factory.createFromPattern(actualFieldType);
				if (initValue != null)
					m_platformType = initValue.GetType();
			}
			m_actualFieldType = actualFieldType;
			m_inheritedField = inheritedField;
		}

		public BizObjField(BizObjField copyField)
		{
			m_parent = copyField.Parent;
			m_parentIndex = copyField.BizObjIndex;

			m_name = copyField.Name;
			m_bizFieldType = copyField.BizType;
			m_defaultValue = copyField.SampleValue;
			m_platformType = copyField.m_platformType;
			m_validators = copyField.m_validators;
			m_actualFieldType = copyField.m_actualFieldType;
			m_inheritedField = copyField.m_inheritedField;
			m_decorators = copyField.m_decorators;
		}

		public Type PlatformType { get { return m_platformType; } }

		public IList Validators { get { return m_validators; } set { m_validators = value; } }

		#region IBizObjField Members

		public string Name
		{
			get { return m_name; }
		}

		public IBizObjDefinition Parent
		{
			get { return m_parent; }
		}

		public BizFieldType BizType
		{
			get { return m_bizFieldType; }
		}

		public string FactoryType
		{
			get { return m_actualFieldType; }
		}

		public object SampleValue
		{
			get
			{
				return m_defaultValue; 
			}
		}

		public object DecoratedSampleValue
		{
			get
			{
				bool dummy = true;
				object val = m_defaultValue;
				if(m_hasStandins)
					foreach (IDecorator deco in m_decorators.Values)
						if (deco.IsStandin)
							((IBizObjDecorator)deco).put(null, this, ref val, null, ref dummy);
				return val;
			}
		}
		public int BizObjIndex
		{
			get { return m_parentIndex; }
		}
		protected internal void setBizObjIndex(int index)
		{
			m_parentIndex = index;
		}

		public bool InheritedField
		{
			get { return m_inheritedField; }
			set { m_inheritedField = value; }
		}

		public string getBizObjType()
		{
			if (m_defaultValue != null)
			{
				if (m_bizFieldType == BizFieldType.BizObj)
					return ((IBizObj)m_defaultValue).type.Name;
				else if (m_bizFieldType == BizFieldType.BizObjList)
					return ((IBizObjList)m_defaultValue).objectType.Name;
			}
			return null;
		}

		public IDictionary Decorators
		{
			get { return m_decorators==null?s_emptyDecos:m_decorators; }
		}

		public bool HasStandInDecorators
		{
			get { return m_hasStandins; }
		}

		public void addDecorator(IDecorator deco)
		{
			if (deco.IsStandin)
				m_hasStandins = true;
			if (m_decorators == null)
				m_decorators = new Hashtable();
			m_decorators[deco.Name] = deco;
		}

		public bool doesUseFullXmlElement()
		{
			if (m_bizFieldType == BizFieldType.NetClass)
				return false;
			else
			{
				if(HasStandInDecorators)
					foreach(IDecorator deco in Decorators.Values)
						if (deco is IBizObjDecorator && !((IBizObjDecorator)deco).doesUseFullXmlElement())
							return false;
				return true;
			}
		}

		#endregion

		#region ICloneable Members

		public object Clone()
		{
			return new BizObjField(this);
		}

		#endregion
	}
}
